from datetime import datetime
from typing import Optional,List
from uuid import UUID, uuid4
from sqlalchemy import Column, DateTime, String, Text, text, desc, func
from sqlmodel import Field, SQLModel
from sqlalchemy.types import Date

from bisheng.database.base import session_getter
from bisheng.database.models.base import SQLModelSerializable

class ScheduleRecordBase(SQLModelSerializable):
    id: UUID = Field(nullable=False, primary_key=True, description='唯一ID')
    station: str = Field(sa_column=Column(String(length=255), default=''), description='站点信息，表明该事件所属的站点')
    line: str = Field(sa_column=Column(String(length=255), default=''), description='线路信息，表明该事件所属的线路')
    date: datetime = Field(sa_column=Column(Date, default=datetime.now), description='事件发生的日期')
    person: str = Field(sa_column=Column(String(length=255), default=''), description='排班人员')
    username: str = Field(sa_column=Column(String(length=255), default=''), description='用户名')
    phone: str = Field(sa_column=Column(String(length=255), default=''), description='电话号码')
    create_time: Optional[datetime] = Field(
        sa_column=Column(DateTime, nullable=False, server_default=text('CURRENT_TIMESTAMP')))
    update_time: Optional[datetime] = Field(sa_column=Column(DateTime,
                                                             nullable=False,
                                                             index=True,
                                                             server_default=text('CURRENT_TIMESTAMP'),
                                                             onupdate=text('CURRENT_TIMESTAMP')))

class ScheduleRecord(ScheduleRecordBase, table=True):
    id: UUID = Field(default_factory=uuid4, primary_key=True, unique=True)



class ScheduleRecordDao:
    @staticmethod
    def get_schedule_record_by_date(date: str) -> List[ScheduleRecord]:
        with session_getter() as session:
            date_obj = datetime.strptime(date, '%Y-%m-%d').date()
            query = session.query(ScheduleRecord).filter(ScheduleRecord.date == date_obj)
            return query.all()